package com.stylefeng.guns.modular.sort.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.node.ZTreeNode;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.sort.model.Sort;
import com.stylefeng.guns.modular.sort.service.ISortService;

import java.util.List;

/**
 * 控制器
 *
 * @author fengshuonan
 * @Date 2018-05-13 10:38:49
 */
@Controller
@RequestMapping("/sort")
public class SortController extends BaseController {

    private String PREFIX = "/sort/sort/";

    @Autowired
    private ISortService sortService;

    /**
     * 跳转到首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "sort.html";
    }

    /**
     * 跳转到添加
     */
    @RequestMapping("/sort_add")
    public String sortAdd() {
        return PREFIX + "sort_add.html";
    }

    /**
     * 跳转到修改
     */
    @RequestMapping("/sort_update/{sortId}")
    public String sortUpdate(@PathVariable Integer sortId, Model model) {
        Sort sort = sortService.selectById(sortId);
        model.addAttribute("item",sort);
        LogObjectHolder.me().set(sort);
        return PREFIX + "sort_edit.html";
    }

    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return sortService.selectList(null);
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Sort sort) {
        sortService.insert(sort);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer sortId) {
        sortService.deleteById(sortId);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Sort sort) {
        sortService.updateById(sort);
        return SUCCESS_TIP;
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{sortId}")
    @ResponseBody
    public Object detail(@PathVariable("sortId") Integer sortId) {
        return sortService.selectById(sortId);
    }


    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> tree(){
        List<ZTreeNode> tree = this.sortService.tree();
        tree.add(ZTreeNode.createParent());
        return tree;
    }


}
